﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace WorldOfMUDra
{
    public partial class Player : ServerEngine.ServerClient
    {
        public void WhereAmICommand(List<string> message)
        {
            if (loggedIn)
            {
                this.SendWorldMessage("You are in " + this.location.Name + ". " + this.Location);
            }
            else
            {
                this.SendWorldMessage("You are in limbo. Please log in.");
            }
        }

        void LogInCommand(List<string> message)
        {
            if (message.Count >= 3)
            {
                string uname = message[1].Trim();
                string unhashedpass = message[2].Trim();
                string hashedpass = PasswordStorageFile.CalculateMD5Hash(unhashedpass);  // Hashes the players' password to keep the password confidential to the player
                if (Globals.PassFile.ContainsUser(uname))   // If the user has logged in before.
                {
                    this.SendWorldMessage("Welcome back " + uname + ". Please wait while we verify your credentials.");
                    if (Globals.PassFile.VerifyPasswordOf(uname, unhashedpass)) // If the user enters the correct password
                    {
                        this.loggedIn = true;
                        this.name = uname;
                        string configfilename = uname + ".xml";
                        this.SendWorldMessage("You have successfully logged in. Now sending you to your previous location.");
                        if (File.Exists(configfilename))
                        {
                            PlayerConfigFile pcf = new PlayerConfigFile(configfilename);
                            this.AddVisitedArea(pcf);
                            this.LoadAbilities(pcf);
                            World.Areas[pcf.LocationID].MoveHere(this);
                        }
                        else
                        {
                            Globals.Mudra.FirstArea.MoveHere(this);
                            this.PlaySound("tull", false);
                            Globals.CreateConfigFor(this);
                        }
                    }
                    else
                    {
                        this.SendWorldMessage("Sorry, but your credentials did not match.");
                    }

                }
                else
                {
                    this.SendWorldMessage("You're new. Let's add you to the adventure shall we?");
                    Globals.PassFile.AddUser(uname, unhashedpass);
                    Globals.CreateConfigFor(this);
                    this.SendWorldMessage("Your password will be '" + unhashedpass + "'. Please log in with your new credentials.");
                    this.loggedIn = true;
                }
            }
            else
            {
                this.SendWorldMessage("The correct usage of login is: login username password");
            }
        }


        void SayCommand(List<string> message)
        {
            if (this.loggedIn)
            {
                if (this.CurrentArea() != null)
                {
                    string strData = string.Join(" ", message.ToArray());
                    if (!(strData.Length < 4)) // This is to avoid people from sending blank spaces to the console
                    {
                        this.CurrentArea().ChatToOccupants(this.name, strData.Substring(3)); // We use the "Substring" function to avoid sending the string "say" to the screen.
                    }
                }
            }
        }

        public void MoveToCommand(List<string> message)
        {
            if (this.loggedIn)
            {
                if (message.Count >= 2)
                {
                    string place = message[1].Trim();
                    if (this.location.LeadsTo(place))
                    {
                        this.SendWorldMessage("You make your way to " + this.location.Places(place).Name + ".");
                        this.location.Places(place).MoveHere(this);
                        this.SendWorldMessage(this.location.Description);
                    }
                    else
                    {
                        this.SendWorldMessage("You seem to have a hard time getting there.");
                    }
                }
                else
                {
                    this.SendWorldMessage("The correct usage of moveto is: moveto <placeid>");
                }
            }
        }


        //Ablities should all be public (so they can be accessed by the methods that give abilities)
        public void LookAtCommand(List<string> message)
        {
            if (this.loggedIn)
            {
                if (message.Count >= 2)
                {
                    string obj = message[1].Trim();
                    if (this.CurrentArea().ContainsItem(obj))
                    {
                        this.SendWorldMessage(this.CurrentArea().GetItem(obj).Description);
                    }
                    else
                    {
                        this.SendWorldMessage("You cannot spot that item.");
                    }
                }
                else
                {
                    this.SendWorldMessage("The correct usage of lookat is: lookat <itemid>");
                }
            }
        }

        public void LookAroundCommand(List<string> message)
        {
            if (this.loggedIn)
            {
                this.SendWorldMessage("You spot " + this.CurrentArea().ItemList);
            }
        }

        public void PickUpCommand(List<string> message)
        {
            if (this.loggedIn)
            {
                if (message.Count >= 2)
                {
                    if (this.CurrentArea().ContainsItem(message[1]))
                    {
                        if (this.CurrentArea().GetItem(message[1]).Obtainable) 
                        {
                            this.Inventory.Add(this.CurrentArea().GetItem(message[1]).ID, this.CurrentArea().GetItem(message[1]));
                        }
                        if (!this.HasPickedUp(CurrentArea().GetItem(message[1])))
                        {
                            CurrentArea().GetItem(message[1]).OnFirstPickUp(this);
                        }
                    }
                }
            }
        }

        public void InventoryCommand(List<string> message)
        {
            if (this.loggedIn)
            {
                this.SendInventory();
            }
        }
    }
}
